package tech.aistar.controller.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Category;
import tech.aistar.entity.CategoryDetail;
import tech.aistar.service.demand.impl.CategoryDetailServiceImpl;
import tech.aistar.service.demand.impl.CategoryServiceImpl;
import tech.aistar.service.services.IService;
import tech.aistar.service.services.impl.ServicesListServiceImpl;
import tech.aistar.vo.ServicesAndNameQueryVo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/10/8
 * @Version: 1.0
 */
@Controller
@RequestMapping("/services")
public class ServicesListController {

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private CategoryDetailServiceImpl categoryDetailService;

    @Autowired
    private ServicesListServiceImpl servicesListService;

    @Autowired
    private IService service;

    //    服务列表
    //默认
    @GetMapping("/serviceListView")
    public String serviceListView(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesAndName();
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }


    /**
     * 根据服务名称模糊查询
     */
    @RequestMapping("/selectAll")
    public String selectAll(HttpServletRequest req,Model model)throws ServletException, IOException {
        //接受查询的参数
        String title = req.getParameter("title");//根据名称模糊查询
        //数据的回显
        req.setAttribute("title",title==null?"":title);

        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByName(title);

        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);

        return serviceCategory(model);
    }
    /**
     * 根据类别查询
     */
    @GetMapping("/selectByCategory")
    public String selectByCategory(HttpServletRequest req ,Model model )throws ServletException, IOException{
        Integer categoryId = Integer.valueOf(req.getParameter("category_id"));
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByCategory(categoryId);
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据详细类别查询
     */
    @GetMapping("/selectByCategoryDetail")
    public String selectByCategoryDetail(HttpServletRequest req ,Model model )throws ServletException, IOException{
        Integer categoryDetailId = Integer.valueOf(req.getParameter("cdid"));
        System.out.println(categoryDetailId);
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByCategoryDetail(categoryDetailId);
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据发布时间查询
     */
    @GetMapping("/servicesBybeginTime")
    public String servicesBybeginTime(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesBybeginTime();
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据成交数降序查询
     */
    @GetMapping("/servicesByCount")
    public String servicesByCount(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByCount();
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据价格降序查询
     */
    @GetMapping("/servicesByPriceDesc")
    public String servicesByPriceDesc(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByPriceDesc();
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据价格升序查询
     */
    @GetMapping("/servicesByPrice")
    public String servicesByPrice(HttpServletRequest req ,Model model )throws ServletException, IOException{
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByPrice();
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }

    /**
     * 根据价格区间查询
     */
    @GetMapping("/servicesByPriceRange")
    public String servicesByPriceRange(HttpServletRequest req ,Model model )throws ServletException, IOException{
        String minPrice = req.getParameter("minPrice");
        Double minPrice1 = Double.valueOf(minPrice);
        System.out.println("minPrice1 = " + minPrice1);
        String maxPrice = req.getParameter("maxPrice");
        Double maxPrice1 = Double.valueOf(maxPrice);
        System.out.println("maxPrice1 = " + maxPrice1);
        List<ServicesAndNameQueryVo> servicesAndNameQueryVoList = servicesListService.findServicesByPriceRange(minPrice1,maxPrice1);
        req.setAttribute("servicesAndNameQueryVoList",servicesAndNameQueryVoList);
        return serviceCategory(model);
    }



//    类别
    @GetMapping("/serviceCategory")
    public String serviceCategory(Model model ){
        List<Category> categoryList =  categoryService.findAll();
        List<CategoryDetail> categoryDetailList = categoryDetailService.findAl();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("categoryDetailList",categoryDetailList);
        return "services/serviceList";
    }

//    @RequestMapping("/serviceList2")
//    public String servicelist(Model model,HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "6") Integer pageSize){
//
//        PageInfo<ServicesAndNameQueryVo> pageInfo = service.findAll(Integer.valueOf(id),Integer.valueOf(pageNum),Integer.valueOf(pageSize));
//
//
//
//        model.addAttribute("pageInfo",pageInfo);
//        return "services/serviceList";
//
//    }
}
